<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%
	request.setAttribute("path", request.getContextPath());
%>
<script type="text/javascript">
$(function(){
	$('#orderTime').timepicker({minuteStep: 30,showMeridian: false});
	addChgValitation();
});

function addChgValitation(){
	
	$("#userName").change(function (){
		check.emptyCheck("userName");
	});
	
	$("#userType").change(function (){
		check.emptyCheck("userType");
	});

	$("#firstOrg").change(function (){
		check.emptyCheck("firstOrg");
	});

	$("#secondOrg").change(function (){
		check.emptyCheck("secondOrg");
	});

	$("#mobile").change(function (){
		check.mobile("mobile");
	});

	$("#phone").change(function (){
		check.phone2("phone");
	});

	$("#address").change(function (){
		check.emptyCheck("address");
	});

	$("#firstCat").change(function (){
		check.emptyCheck("firstCat");
	});

	$("#secondCat").change(function (){
		check.emptyCheck("secondCat");
	});

	$("#productName").change(function (){
		check.emptyCheck("productName");
	});

	$("#purchaseDate").change(function (){
		check.emptyCheck("purchaseDate");
	});

	$("#purchasePrice").change(function (){
		check.floatNum2("purchasePrice");
	});

	$("#orderType").change(function (){
		check.emptyCheck("orderType");
	});

	$("#urgentLevel").change(function (){
		check.emptyCheck("urgentLevel");
	});

	$("#mandatoryLevel").change(function (){
		check.emptyCheck("mandatoryLevel");
	});

	$("#orderDate").change(function (){
		check.emptyCheck("orderDate");
	});

	$("#orderUser").change(function (){
		check.emptyCheck("orderUser");
	});

	$("#orderMobile").change(function (){
		check.emptyCheck("orderMobile");
	});	
}


function validateUser(){

	return check.emptyCheck("userName") & 
			check.emptyCheck("userType") & 
			check.emptyCheck("firstOrg") & 
			check.emptyCheck("secondOrg") & 
			check.mobile("mobile") & 
			check.emptyCheck("address") &
			check.phone2("phone");
			
}

function validateProduct(){
    var val = $("#customerId").val();
    if(common.isEmpty(val)){
    	common.modalGlobalMessage("请先设置客户信息","错误"); 
    	return false;
    }
	return check.emptyCheck("firstCat") &  
		   check.emptyCheck("secondCat") & 
		  check.emptyCheck("productName") &  
		  check.emptyCheck("purchaseDate") & 
		  check.floatNum2("purchasePrice");
}

function validateOrder(){
    var val = $("#productId").val();
    if(common.isEmpty(val)){
    	common.modalGlobalMessage("请先设置产品信息","错误"); 
    	return false;
    }
	return check.emptyCheck("orderType") & 
		check.emptyCheck("urgentLevel") & 
		check.emptyCheck("mandatoryLevel") & 
		(check.emptyCheck("orderDate") && check.emptyCheck("orderTime")) ;
}
function saveCustomer(){
	if(validateUser()){
		var uri = "${root}/order/customer/update"
		submitForm("user",uri,"customerForm","保存客户失败","",'saveCustomerSucc()');
	}
}
function saveCustomerSucc( id ){

	$("#accordionUser").collapse('hide');
	$("#accordionProduct").collapse('show');
	$("#customerId").val(id);
}

function saveProduct(){

	if(validateProduct()){
		var uri = "${root}/order/product/update"
			submitForm("product",uri,"productForm","保存客户产品失败","",'saveProductSucc()');
	}
}
function saveProductSucc(id){

	$("#accordionProduct").collapse('hide');
	$("#accordionOrder").collapse('show');
	$("#productId").val(id);
}

function saveOrder(){

	if(validateOrder()){
		var uri = "${root}/order/update"
			submitForm("order",uri,"orderForm","保存订单失败","",'saveOrderSucc()');
	}
}
function saveOrderSucc(id){

	common.reload("${root}/order/list/init?loc=order_dispatch_list&h=h05&n=n03");
}

function submitForm( type ,url ,formId,errMsg,succMsg,succFun){
	
	var formData = $("#"+formId).serialize();
	common.progressDialog("...提交中...");
	$.post(url, formData, function(result) {
		common.closeProgressDialog();
		 if(result.type == "0"){
			 var mvcValidtorResult = result.content;
			 for(var key in mvcValidtorResult.fieldErrors){
				 common.showClientErrorTip(key,mvcValidtorResult.fieldErrors[key]);
			 }
			 var globalMsg = mvcValidtorResult.globalMessage;
			 if(globalMsg){
				 common.modalGlobalMessage(globalMsg,"错误");  
			 }
		 } else if(result.type == "1"){
			 common.modalGlobalMessage(errMsg,"错误"); 
		 }else if(result.type == "2"){
			 common.modalGlobalMessage("系统出现异常了","系统异常");  
		 }else if(result.type == "3"){
			 if(succMsg){
				 common.moalYesNoDialog(succMsg,"成功",succFun);
			 }else{
				if(type =="user"){
					saveCustomerSucc(result.content)
				}
				if(type =="product"){
					saveProductSucc(result.content)
				}
				if(type =="order"){
					saveOrderSucc(result.content)
				}
			 }
		 }else{
			 common.modalGlobalMessage("系统出现异常了","系统异常");  
		 }
	},"json");
	
}

function searchCustomer(){

	var name = $.trim($("#userName").val());
	var mobile = $.trim($("#userMobile").val());
	if(common.isEmpty(name) && common.isEmpty(mobile)){
		common.modalGlobalMessage("请输入用户名 或者 手机!","错误");  
		return false;
	}
	common.progressDialog("...查询中...");
	var uri = "${root}/order/customer/search";
	$.post(uri, {name:name,mobile:mobile}, function(user) {
		common.closeProgressDialog();
		if(user && user.id){
			$("#userId").val(user.id);
			$("#customerId").val(user.id);
			$("#userName").val(user.name);
			$("#mobile").val(user.mobile);
			$("#userType").val(user.type);
			$("#address").val(user.address);
			$("#zip").val(user.zip);
			$("#gender").val(user.gender);
			$("#phone").val(user.phone);
			$("#email").val(user.email);
			$("#comment").val(user.comments);
			initFirstOrg("firstOrg",user.firstOrg.id,"secondOrg",user.secondOrg.id,false);
		}else{
			common.modalGlobalMessage("查询的客户不存在!","");  
		}
	});
}

function searchProduct(){
	
	var customerId = $.trim($("#customerId").val());
	var productName = $.trim($("#productName").val());
	if(common.isEmpty(customerId)){
		common.modalGlobalMessage("请先设置用户信息!","错误");  
		return false;
	}
	if(common.isEmpty(productName)){
		common.modalGlobalMessage("请输入型号!","错误");  
		return false;
	}
	common.progressDialog("...查询中...");
	var uri = "${root}/order/product/search";
	$.post(uri, {customerId:customerId,productName:productName}, function(cp) {
		common.closeProgressDialog();
		if(cp && cp.id){

			initFirstCat("firstCat",cp.product.firstCat.id,"secondCat",cp.product.secondCat.id,false);

			$("#productId").val(cp.id);
			$("#productName").val(cp.product.name);
			$("#purchaseDate").val(cp.purchaseDate);
			
			$("#purchasePrice").val(cp.purchasePrice);
			$("#recepitNo").val(cp.recepitNo);
			$("#barCode").val(cp.barCode);
			$("#mandatoryNo").val(cp.mandatoryNo);
			$("#salesShop").val(cp.salesShop);
			$("#salesContact").val(cp.salesContact);
			$("#productComment").val(cp.comments);
			
		}else{
			common.modalGlobalMessage("当前客户无产品信息!","");  
		}
	});

	
}
</script>
<jsp:include page="../base/master_js.jsp"></jsp:include>
<div class="accordion" >
  <div class="accordion-group">
    <div class="accordion-heading">
      <h4 >
        <a class="accordion-toggle" data-toggle="collapse"data-parent="#accordion" href="#accordionUser" style="padding:0px">
        	<strong>&nbsp;客户信息</strong>
        </a>
      </h4>
    </div>
    <div id="accordionUser" class="accordion-body collapse in">
      <div class="accordion-inner">
			<form class="form-horizontal" id="customerForm" role="form">
			<input type="hidden" value="" name="userId"/>
			<table class="table table-hover">
					<tr>
						<th>客户姓名:</th>
						<td>
							<input type="text" class="input-xlarge" name="userName" id="userName" maxlength="50"/>
							<span class="text-error">*</span>
						</td>
						<th>手机:</th>
						<td>
							<input type="text" class="input-xlarge" name="mobile" id="mobile" maxlength="50"/>
							<button type="button" class="btn btn-success" title="按姓名或者手机查询" onclick="searchCustomer()">查询</button>
							<span class="text-error">*</span>
						</td>							
					</tr>
					<tr>
						<th>大区:</th>
						<td>
							<select id="firstOrg" name="firstOrg" class="input-xlarge" onchange="firstOrgChange(this,'secondOrg',false)">
								<option value=""></option>
								<c:forEach var="item" items="${firstOrgs}">
									<option value="${item.id }"><c:out value="${item.name }"/></option>
								</c:forEach>
							</select>
							<span class="text-error">*</span>
						</td>
						<th>办事处:</th>
						<td>
							<select id="secondOrg" name="secondOrg" class="input-xlarge" >
							</select>
							<span class="text-error">*</span>
						</td>
					</tr>
					<tr>
						<th>客户类别:</th>
						<td>
							<select id="userType" name="userType" class="input-xlarge">
								<option value=""></option>
								<c:forEach var="item" items="${customerTypeList}">
									<option value="${item.id }"><c:out value="${item.name }"/></option>
								</c:forEach>
							</select>
							<span class="text-error">*</span>
						</td>					
						<th>性别:</th>
						<td>
							<select name="gender" id="gender" class="input-xlarge" >
									<option value=""></option>
									<option value="0">男</option>
									<option value="1">女</option>
							</select>
						</td>
					</tr>
					<tr>
						<th>地址:</th>
						<td>
							<input type="text" class="input-xxlarge" name="address" id="address" /><span class="text-error">*</span>
						</td>
						<th>邮编:</th>
						<td >
					    	<input type="text" class="input-xlarge" name="zip" id="zip" />
						</td>
					</tr>					
					<tr>
						<th>座机:</th>
						<td>
							<input type="text" class="input-xlarge" name="phone" id="phone" />
						</td>
						<th>EMAIL:</th>
						<td >
					    	<input type="text" class="input-xlarge"  id="email" name="email" >
						</td>
					</tr>
					<tr>
						<th>备注:</th>
						<td colspan="2"><input type="text" class="input-xxlarge" name="comment" id="comment" maxlength="500"/></td>
						<td>
							<button type="button" class="btn btn-success" onclick="saveCustomer()">保存</button>
						</td>
					</tr>
				</table>
			</form>
      </div>
    </div>
  </div>
  
<div class="accordion-group">
    <div class="accordion-heading">
      <h4 >
        <a class="accordion-toggle" data-toggle="collapse"data-parent="#accordion" href="#accordionProduct" style="padding:0px">
        	<strong>&nbsp;产品信息</strong>
        </a>
      </h4>
    </div>
    <div id="accordionProduct" class="accordion-body collapse">
      <div class="accordion-inner">
			<form class="form-horizontal" id="productForm" role="form">
			<input type="hidden" name="customerId" id="customerId">
			<input type="hidden" name="id" id="id">
			<table class="table table-hover">
				<tr>
						<th>机型:</th>
						<td>
							<input type="text" class="input-xlarge" name="productName" id="productName" maxlength="50" >
							<button type="button" class="btn btn-success" title="按机型查询，请先设置用户信息" onclick="searchProduct()">查询</button>
							<span class="text-error">*</span>
						</td>
						<th>购买日期:</th>
						<td>
							<input type="text" data-date="${today}"  data-date-format="yyyy-mm-dd" class="input-xlarge datepicker" id="purchaseDate" name="purchaseDate" >
							<span class="text-error">*</span>
						</td>						
					</tr>			
				  <tr>
					<th>产品线:</th>
					<td>
						<select id="firstCat" name="firstCat" class="input-xlarge" onchange="firstCatChange(this,'secondCat')">
							<option value=""></option>
							<c:forEach var="item" items="${firstCats}">
								<option value="${item.id }"><c:out value="${item.name }"/></option>
							</c:forEach>
						</select>
						<span class="text-error">*</span>
					</td>
					<th>品牌:</th>
					<td>
						<select id="secondCat" name="secondCat" class="input-xlarge" >
						</select>
						<span class="text-error">*</span>
					</td>
				  </tr>
					<tr>
						<th>购买价格:</th>
						<td>
							<input type="text" class="input-xlarge" name="purchasePrice" id="purchasePrice" maxlength="50"/>
							
						</td>
						<th>发票号码:</th>
						<td >
					    	<input type="text" class="input-xlarge" name="recepitNo" id="recepitNo" />
						</td>
					</tr>					
					<tr>
						<th>机器编号:</th>
						<td>
							<input type="text" class="input-xlarge" name="barCode" id="barCode" />
						</td>
						<th>保修卡号:</th>
						<td >
					    	<input type="text" class="input-xlarge"  id="mandatoryNo" name="mandatoryNo" >
						</td>
					</tr>
					<tr>
						<th>售货单位:</th>
						<td>
							<input type="text" class="input-xlarge" name="salesShop" id="salesShop" />
						</td>
						<th>售货电话:</th>
						<td >
					    	<input type="text" class="input-xlarge"  id="salesContact" name="salesContact" >
						</td>
					</tr>					
					<tr>
						<th>备注:</th>
						<td colspan="2"><input type="text" class="input-xxlarge" name="productComment" id="productComment" maxlength="500"/></td>
						<td>
							<button type="button" class="btn btn-success" onclick="saveProduct()">保存</button>
						</td>
					</tr>
				</table>
			</form>
      </div>
    </div>
  </div>  
  
  <div class="accordion-group">
    <div class="accordion-heading">
      <h4 >
        <a class="accordion-toggle" data-toggle="collapse"data-parent="#accordion" href="#accordionOrder" style="padding:0px">
        	<strong>&nbsp;服务信息</strong>
        </a>
      </h4>
    </div>
    <div id="accordionOrder" class="accordion-body collapse">
      <div class="accordion-inner">
			<form class="form-horizontal" id="orderForm" role="form">
			<input type="hidden" name="productId" id="productId">
			<table class="table table-hover">
					<tr>
						<th>服务类型:</th>
						<td>
							<select id="orderType" name="orderType" class="input-xlarge">
								<option value=""></option>
								<c:forEach var="item" items="${orderTypeList}">
									<option value="${item.id }"><c:out value="${item.name }"/></option>
								</c:forEach>
							</select>
							<span class="text-error">*</span>
						</td>
						<th>紧急度:</th>
						<td>
							<select id="urgentLevel"  name="urgentLevel" class="input-xlarge">
								<option value=""></option>
								<c:forEach var="item" items="${urgentLevelList}">
									<option value="${item.id }"><c:out value="${item.name }"/></option>
								</c:forEach>
							</select>
							<span class="text-error">*</span>
						</td>
					</tr>
					<tr>
						<th>维修等级:</th>
						<td>
							<select id="mandatoryLevel" name="mandatoryLevel" class="input-xlarge">
								<option value=""></option>
								<c:forEach var="item" items="${mandatoryLevelList}">
									<option value="${item.id }"><c:out value="${item.name }"/></option>
								</c:forEach>
							</select>						
						</td>
						<th>预约日期:</th>
						<td>
							<input type="text" data-date="${today}"  data-date-format="yyyy-mm-dd" class="input-medium datepicker" id="orderDate" name="orderDate" >
							<input type="text" id="orderTime" name="orderTime" class="input-small"/>
							<span class="text-error">*</span>
						</td>						
					</tr>
					<tr>
						<th>报修人:</th>
						<td>
							<input type="text" class="input-xlarge"  id="orderUser" name="orderUser" >
						</td>					
						<th>联系电话:</th>
						<td>
							<input type="text" class="input-xlarge" name="orderMobile" id="orderMobile" maxlength="50"/>
						</td>
					</tr>
					<tr>
						<th>故障描述:</th>
						<td colspan="3"><input type="text" class="input-xxlarge" name="hitchComments" id="hitchComments" maxlength="500"/></td>
					</tr>					
					<tr>
						<th>备注:</th>
						<td colspan="2"><input type="text" class="input-xxlarge" name="comments" id="comments" maxlength="500"/></td>
						<td>
							<button type="button" class="btn btn-success" onclick="saveOrder()">保存</button>
						</td>
					</tr>
				</table>
			</form>
      </div>
    </div>
  </div>  
  </div>	

